package ${serviceImpl};

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import ${mapper}.${className}Mapper;
import ${service}.I${className}Service;
import com.open.model.Sort;
import com.open.model.Page;
import ${entity}.${className};

/**
 * $!codeName
 *
 * @author $!currentUser
 * @date $!date
 */
@Service(value = "$!{lowerName}Service")
@Slf4j
public class ${className}ServiceImpl implements I${className}Service {

	private ${className}Mapper ${lowerName}Mapper;

    @Autowired
    public void set${className}Mapper(${className}Mapper ${lowerName}Mapper) {
        this.${lowerName}Mapper = ${lowerName}Mapper;
    }

    @Override
    @Transactional()
    public Boolean save(${className} model) {
        return ${lowerName}Mapper.save(model);
    }

    @Override
    @Transactional()
    public Boolean update(${className} model) {
        return ${lowerName}Mapper.update(model);
    }

    @Override
    @Transactional()
    public Boolean batchSave(List<${className}> models) {
        return ${lowerName}Mapper.batchSave(models);
    }

    @Override
    @Transactional()
    public Boolean batchUpdate(List<${className}> models) {
        return ${lowerName}Mapper.batchUpdate(models);
    }

	@Override
	public Page<${className}> findPage(Page<${className}> page, ${className} item) {
        Map<String, Object> map = new HashMap<String, Object>(4);
        int startPoint = (page.getIndex() - 1) * page.getSize();
        map.put("orderBy", new Sort());
        map.put("item", item);
        map.put("startPoint", startPoint);
#if($Dialect=="MSSQL")
        map.put("endPoint", page.getIndex()*page.getSize());
#elseif($Dialect=="MySQL")
        map.put("endPoint", page.getSize());
#end
        Integer count = ${lowerName}Mapper.findPageCount(map);
        if (count > startPoint) {
            page.setItems(${lowerName}Mapper.findPage(map));
        } else {
            page.setItems(new ArrayList<${className}>());
        }
        page.setTotal(count);
        return page;
    }

	@Override
    public List<${className}> findByModel(${className} item) {
        Map<String, Object> map = new HashMap<String, Object>(1);
        map.put("item",item);
        return ${lowerName}Mapper.findByModel(map);
    }

	@Override
	public ${className} findByPk(${pk.jdbcType} pk) {
        return ${lowerName}Mapper.findByPk(pk);
    }

    @Override
    public Integer countByModel(${className} item) {
        Map<String, Object> map = new HashMap<String, Object>(1);
        map.put("item",item);
        return ${lowerName}Mapper.findPageCount(map);
    }

    @Override
    @Transactional()
    public Boolean remove(List<${pk.jdbcType}> pks) {
        return ${lowerName}Mapper.remove(pks) > 0;
    }

    @Override
    @Transactional()
    public Boolean delete(${pk.jdbcType} pk) {
        return ${lowerName}Mapper.delete(pk) > 0;
    }
}